﻿using System;
using System.Collections.Generic;
using COFE.Core;
using COFE.Core.Lister;
using NUnit.Framework;
using COFE.Core.Filter;

namespace COFE.Data.DBLink
{
    public class DBLinkDirectoryController : DirectoryControllerBase<Entry>
    {
        #region Constructor

        public DBLinkDirectoryController(DBLinkDirectoryLister dirLister)
            : base(dirLister)
        {
            _createDirectory = (parentEntry, dirName) =>
                {
                    string newDirPath = COFEPath.Combine(parentEntry.ParsePath, dirName);
                    var pp = new DBLinkTempPropertyProvider(parentEntry.PrimaryVolumeId, newDirPath);
                    return COFEDBShared.DBEntryManager.UpdateEntry(pp);
                };

        }

        #endregion

        #region Methods

        public override void Copy(IInternalFileSystemInfoExA entry, IInternalDirectoryInfoExA targetDir, bool raiseAlert)
        {
            var parentEntry = getEmbeddedItem(targetDir);
            ICustomEntryList entryList = COFEShared.EntryListRegistrar.GetDBLinkEntryList(COFEShared.CurrentUserProfile, parentEntry) as ICustomEntryList;
            Assert.IsNotNull(entryList);
            entryList.AddRange(new List<IFileSystemInfo> { entry });
        }

        public override void Delete(IInternalFileSystemInfoExA entry, bool raiseAlert)
        {
            if (entry.ListerKey == this.ListerKey || (entry.Parent != null && entry.Parent.ListerKey == this.ListerKey))
            {
                var parentEntry = getEmbeddedItem(entry.Parent as IInternalDirectoryInfoExA);
                if (parentEntry != null)
                {
                    ICustomEntryList parentEntryList = 
                        COFEShared.EntryListRegistrar.GetDBLinkEntryList(COFEShared.CurrentUserProfile, parentEntry) as ICustomEntryList;
                    parentEntryList.Delete(new GuidEntryFilter(entry.GetId()));
                }
            }
            else throw new NotSupportedException();
        }

        public override void Move(IInternalFileSystemInfoExA entry, IInternalDirectoryInfoExA targetDir, bool raiseAlert)
        {
            Copy(entry, targetDir, false);
            if (entry.Parent.ListerKey == this.ListerKey)
            {
                Delete(entry, false);
            }

            if (raiseAlert)
            {
                
            }
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        #endregion
    }
}
